interface ExecutionResult<T> {
    result?: T;
    exceededLimit: boolean;
    executionsLeft: number;
}

export const limitFunctionExecution = <T extends (...args: any[]) => any>(
    func: T,
    limit: number,
    interval: number
) => {
    let count = 0;
    let lastExecuted = 0;

    return (...args: Parameters<T>): ExecutionResult<ReturnType<T>> => {
        const now = Date.now();
        if (now - lastExecuted >= interval) {
            count = 1;
            lastExecuted = now;
            return { result: func(...args), exceededLimit: false, executionsLeft: limit - 1 };
        } else {
            if (count < limit) {
                count++;
                return { result: func(...args), exceededLimit: false, executionsLeft: limit - count };
            } else {
                return { exceededLimit: true, executionsLeft: 0 };
            }
        }
    };
};